//
//  GRHUD.m
//  YC_GRice
//
//  Created by LiuFeng on 2018/7/20.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRHUD.h"
#import <MBProgressHUD.h>

@implementation GRHUD

+ (void)showMessage:(NSString *)msg {
    [self showAutomaticMsg:msg];
}

+ (void)showProgress:(NSString *)msg {
    [self showManualProgress:msg];
}

+ (void)showErrorMsg:(NSString *)errorMsg {
    [self showAutomaticMsg:errorMsg];
}

+ (void)showMessage:(NSString *)msg atView:(UIView *)view {
    if (view) {
        [self MBProgressHudShowMsg:msg atView:view];
    }else {
        [self showAutomaticMsg:msg];
    }
}

+ (void)showProgress:(NSString *)msg atView:(UIView *)view {
    if (view) {
        [self MBProgressHudShowProgress:msg atView:view];
    }else {
        [self showManualProgress:msg];
    }
}


+ (void)showCustomHud:(NSString *)msg icon:(NSString *)iconName {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    if (!window) {
        return;
    }
    [self MBProgressHudCustom:msg icon:iconName atView:window];
}

+ (void)showCustomHud:(NSString *)msg icon:(NSString *)iconName atView:(UIView *)view {
    if (view) {
        [self MBProgressHudCustom:msg icon:iconName atView:view];
    }else {
        [self showCustomHud:msg icon:iconName];
    }
}

static NSString * const animationKey = @"basisAnimation";
+ (void)showLoading:(NSString *)msg {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [self showLoading:msg atView:window];
}

+ (void)showLoading:(NSString *)msg atView:(UIView *)view {
    UIImageView *imageView  = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loading"]];
    
    CABasicAnimation *basisAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    basisAnimation.toValue  = [NSNumber numberWithFloat:M_PI * 2];
    basisAnimation.duration = 1;
    [basisAnimation setCumulative:YES];
    basisAnimation.repeatCount = MAXFLOAT;
    
    [imageView.layer addAnimation:basisAnimation forKey:animationKey];
    
    [GRHUD MBProgressHudLoading:msg customView:imageView atView:view];
}

#pragma Mark ---------------  隐藏HUD  ------------------
+ (void)dismissHUD {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [self removeHudInView:window];
}

+ (void)dismissHUD:(UIView *)view {
    if (!view) {
        [self dismissHUD];
    }else {
        [self removeHudInView:view];
    }
}

+(void)removeHudInView:(UIView *)view {
    NSArray *array = view.subviews;
    
    for (UIView *view in array) {
        if([view isKindOfClass:[MBProgressHUD class]]) {
            MBProgressHUD *hud = (MBProgressHUD*)view;
            if ([hud.customView.layer.animationKeys containsObject:animationKey]) {
                [hud.customView.layer removeAnimationForKey:animationKey];
            }
            [hud hideAnimated:YES];
        }
    }
}


/**
 展示提示信息 （自动消失）

 @param msg <#msg description#>
 */
+ (void)showAutomaticMsg:(NSString *)msg {
    if (NSStringNotNULL(msg) && msg.length > 0) {
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        if (!window) {
            return;
        }
        [self MBProgressHudShowMsg:msg atView:window];
    }
}


/**
 展示进度 （手动处理）

 @param progress <#progress description#>
 */
+ (void)showManualProgress:(NSString *)progress {
    if (NSStringNotNULL(progress) && progress.length > 0) {
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        if (!window) {
            return;
        }
        [self MBProgressHudShowProgress:progress atView:window];
    }
}

+ (MBProgressHUD *)MBProgressHudShowMsg:(NSString *)msg atView:(UIView *)view {
    if (NSStringNotNULL(msg) && msg.length > 0) {
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
        hud.mode              = MBProgressHUDModeText;
        hud.detailsLabel.text = msg;
        hud.removeFromSuperViewOnHide = YES;
        hud.bezelView.style   = MBProgressHUDBackgroundStyleBlur;
        hud.bezelView.backgroundColor = [UIColor blackColor];
        hud.contentColor      = [UIColor whiteColor];
        hud.detailsLabel.font = [UIFont systemFontOfSize:15];
        [hud hideAnimated:YES afterDelay:1.f];
        return hud;
    }
    return nil;
}


+ (MBProgressHUD *)MBProgressHudShowProgress:(NSString *)msg atView:(UIView *)view {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.removeFromSuperViewOnHide = YES;
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    hud.contentColor    = KGMTHEM_COLOR;
    hud.detailsLabel.font = [UIFont boldSystemFontOfSize:15];
    hud.detailsLabel.textColor = [UIColor whiteColor];
    
    if (NSStringNotNULL(msg)) {
        hud.detailsLabel.text = msg;
    }
    
    return hud;
}

+ (MBProgressHUD *)MBProgressHudCustom:(NSString *)msg icon:(NSString *)icon atView:(UIView *)view {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:icon]];
    MBProgressHUD *hud = [self MBProgressHudLoading:msg customView:imageView atView:view];
    
    [hud hideAnimated:YES afterDelay:1];
    
    return hud;
}

+ (MBProgressHUD *)MBProgressHudLoading:(NSString *)msg customView:(UIView *)customView atView:(UIView *)view {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.removeFromSuperViewOnHide = YES;
    
    hud.mode       = MBProgressHUDModeCustomView;
    hud.margin     = 10.f;
    hud.customView = customView;
    hud.bezelView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    
    hud.detailsLabel.text = msg;
    hud.detailsLabel.font = [UIFont systemFontOfSize:13];
    hud.detailsLabel.textColor = [UIColor whiteColor];
    
    return hud;
}


@end
